package com.itheima.organization.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.aom.pojo.Aom;
import com.itheima.aom.pojo.AomOrder;
import com.itheima.aom.pojo.QueryPageBean;
import com.itheima.organization.dao.AomMapper;
import com.itheima.organization.dao.AomOrderMapper;
import com.itheima.organization.service.AomOrderService;
import com.itheima.organization.service.AomService;
import com.itheima.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AomOrderServiceImpl implements AomOrderService {

    @Autowired
    private AomOrderMapper aomOrderMapper;
    @Autowired
    private AomService aomService;
    @Autowired
    private AomMapper aomMapper;

    /**
     * 分页查询
     * @param map
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<AomOrder> findPage(Map<String, Object> map, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Example example = createExample(map);
        List<AomOrder> aomOrders = aomOrderMapper.selectByExample(example);
        return (Page<AomOrder>) aomOrders;
    }

    @Override
    public List<Aom> findByPhoneNu(String phone) {
        Example example = new Example(Aom.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", phone);
        List<Aom> aomList = aomMapper.selectByExample(example);
        return aomList;
    }

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Override
    public AomOrder findById(Long id) {
        return aomOrderMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新订单
     * @param aomOrder
     */
    @Override
    public void update(AomOrder aomOrder) {
        //防止错误更新导致关联失效
        aomOrder.setAomid(null);
        aomOrderMapper.updateByPrimaryKeySelective(aomOrder);
    }


    /**
     * 新增订单
     * @param aomOrder
     */
    @Override
    public void add(AomOrder aomOrder) {
        aomOrder.setStarttime(DateUtils.dateToString(new Date()));
        Long aomid = aomOrder.getAomid();
        Aom aom = aomService.findById(aomid);
        if (aom == null){
            return;
        }
        //更新Order表
        aomOrderMapper.insertSelective(aomOrder);

        //更新中间表 判段中间表是否存在数据
        List<Map> list = aomMapper.selectByAomId(aomid);
        if (list.isEmpty()){
            aomMapper.insertTo(aomid, aomOrder.getId());
        }else {
            aomMapper.updateTo(aomid, aomOrder.getId());
        }
    }

    private Example createExample(Map<String, Object> searchMap){
        Example example = new Example(AomOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null){
            //农业组织订单id
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))){
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            //农业组织id
            if (searchMap.get("aomid") != null && !"".equals(searchMap.get("aomid"))){
                criteria.andEqualTo("aomid", searchMap.get("aomid"));
            }
            //开始时间
            if (searchMap.get("starttime") != null && !"".equals(searchMap.get("starttime"))){
                criteria.andEqualTo("starttime", searchMap.get("starttime"));
            }
            //结束时间
            if (searchMap.get("endtime") != null && !"".equals(searchMap.get("endtime"))){
                criteria.andEqualTo("endtime", searchMap.get("endtime"));
            }
            //最终时间
            if (searchMap.get("lasttime") != null && !"".equals(searchMap.get("lasttime"))){
                criteria.andEqualTo("lasttime", searchMap.get("lasttime"));
            }
            //支付金额
            if (searchMap.get("paymoney") != null && !"".equals(searchMap.get("paymoney"))){
               // criteria.andEqualTo("phone", searchMap.get("phone"));
                String paymoney = (String) searchMap.get("paymoney");
                String[] split = paymoney.split("-");
                if (split[0].equals("*") && split[1].equals("*")){
                    return example;
                }else {
                    if (split[0].equals("*")){
                        criteria.andLessThan("paymoney", split[1]);
                    }else if (split[1].equals("*")){
                        criteria.andGreaterThan("paymoney",split[0]);
                    }else {
                        criteria.andBetween("paymoney", split[0], split[1]);
                    }
                }
            }
        }
        return example;
    }

}
